package com.aps.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aps.entity.ApsExeption;
import com.aps.entity.domain.dish.Dish;
import com.aps.entity.dto.PosSyncDish;
import com.aps.entity.eunm.ResultType;
import com.aps.page.BaseQuery;
import com.aps.page.Pagination;
import com.aps.service.IDishService;
import com.aps.service.rep.DishRep;
import com.aps.util.JdbcQueryUtil;

/**
 * @author liaoguangan
 * @description <>
 * @date 2018/4/2 18:41
 */

@Service
public class DishServiceImpl implements IDishService {

    @Autowired
    private DishRep dishRep;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * POS向APS同步菜品信息
     *
     * @param posSyncDish
     */
    @Override
    public void save(List<PosSyncDish> posSyncDish) {
        List<Dish> dishes = new ArrayList<>();
        for (PosSyncDish d : posSyncDish) {
            dishes.add(d.build());
        }
        dishRep.save(dishes);
    }

    /**
     * 查询菜品列表
     *
     * @param baseQuery
     * @return
     */
    @Override
    public Pagination<Dish> listDishes(BaseQuery baseQuery) {
        String sql = " select ad.*, adts.`name` dish_type_name, adt.`name` device_type_name, ast.`name` store_name from aps_dish ad "
                + " LEFT JOIN aps_dish_type_seq adts on ad.dish_type_code = adts.`code` "
                + " LEFT JOIN aps_device_type adt on ad.device_type_code = adt.`code` "
                + " LEFT JOIN aps_store ast on ad.store_code = ast.`code` ";

        return new Pagination<Dish>().search(jdbcTemplate, Dish.class, sql, new Object[] {},
                baseQuery, "ad");
    }

    /**
     * 查询菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public Dish findById(String id) {
        String sql = " select ad.*, adts.`name` dish_type_name, adt.`name` device_type_name, ast.`name` store_name from aps_dish ad "
                + " LEFT JOIN aps_dish_type_seq adts on ad.dish_type_code = adts.`code` "
                + " LEFT JOIN aps_device_type adt on ad.device_type_code = adt.`code` "
                + " LEFT JOIN aps_store ast on ad.store_code = ast.`code` where ad.id=?";
        return new JdbcQueryUtil<>(Dish.class).query(jdbcTemplate, sql, new Object[] { id });
    }

    /**
     * 更新菜品信息
     *
     * @param dish
     */
    @Override
    public void update(Dish dish) {
        Dish d = dishRep.findOne(dish.getId());
        if (null == d) {
            throw new ApsExeption(ResultType.DISH_NOT_EXIST);
        }

        d.setDeviceTypeCode(dish.getDeviceTypeCode());
        d.setMerge(dish.getMerge());
        d.setExpTime(dish.getExpTime());
        d.setExpTimeUnit(dish.getExpTimeUnit());
        d.setProcessTime(dish.getProcessTime());
        d.setProcessTimeUnit(dish.getProcessTimeUnit());
        d.setDishSpecCode(dish.getDishSpecCode());

        dishRep.save(d);
    }
}
